import asyncio
import json
import time

from curl_cffi import requests
import subprocess
from functools import partial

subprocess.Popen = partial(subprocess.Popen, encoding='utf-8')
import execjs
import hashlib

with open('h5st515.js', 'r', encoding='utf-8') as f:
    js = f.read()

js_com = execjs.compile(js)

def sha_256(message):
    return hashlib.sha256(message.encode('utf-8')).hexdigest()


def convert_cookie_dict_to_string(cookie_dict):
    # Join each key-value pair with '=' and then join all pairs with '; '
    cookie_string = '; '.join([f"{key}={value}" for key, value in cookie_dict.items()])
    return cookie_string
url = "https://api.m.jd.com/"


def generate_h5st(params):
    # 解析原始 body
    body_str = params['body']
    body = json.loads(body_str) if isinstance(body_str, str) else body_str

    # 创建用于 SHA256 加密的 boddy
    boddy = {
        "type": body['type'],
        "area": body['area'],
    }

    # 创建用于生成 h5st 的 a 对象
    a = {
        "appid": params.get('appid', 'item-v3'),
        "body": sha_256(json.dumps(boddy, separators=(',', ':'))),
        "clientVersion": params.get('clientVersion', '1.0.0'),
        "client": params.get('client', 'pc'),
        "functionId": params.get('functionId', 'pc_stocks'),
        "t": int(time.time() * 1000),  # 使用当前时间戳
    }

    # 调用 JS 的 PSign.sign 方法生成 h5st
    h5st = js_com.call('getH5st', a)

    return h5st
headers = {
    "accept": "application/json, text/javascript, */*; q=0.01",
    "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,ar;q=0.7",
    "cache-control": "no-cache",
    "origin": "https://item.jd.com",
    "pragma": "no-cache",
    "priority": "u=1, i",
    "referer": "https://item.jd.com/",
    "sec-ch-ua": "\"Not)A;Brand\";v=\"8\", \"Chromium\";v=\"138\", \"Microsoft Edge\";v=\"138\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\"",
    "sec-fetch-dest": "empty",
    "sec-fetch-mode": "cors",
    "sec-fetch-site": "same-site",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0",
    "x-referer-page": "https://item.jd.com/10155301055086.html",
    "x-rp-client": "h5_1.0.0"
}
cookies = {}
url = "https://api.m.jd.com/stocks"
params = {
    "appid": "item-v3",
    "functionId": "pc_stocks",
    "client": "pc",
    "clientVersion": "1.0.0",
    "t": str(int(time.time() * 1000)),
    "body": "{\"type\":\"getstocks\",\"area\":\"19_1607_4773_62123\"}",
    "h5st": "20250718151312986;pp999ia3xjqm3301;fb5df;tk03w8e161b8d18nQdIFSLdB1ejGV4UvZISMqTQHe9p5MiiOMiqGYlE1Do1KKwZwD6_vJbVPwhAdO377t1HoA4P9isif;84c26bb26575876d0375d21fb584fdbf;5.1;1752822786986;ri_uxFOm22ciAuLmOGLm9aHWMusmk_Mm9u7i76YSEN3iNJYg1KrS_lsm0msSIlsmOGuj4qrm0mMTLhImOuMsCm8hMtbgNhog4a7i2aoi1ebW_u7hJZog4W7WNdoV3mrhNlsm0msSo94VMZ4RMusmk_Mm8mohIRrgMRri2KLV2m7W1e4i1iYi5ebi1eIiMJbh9qImOGLm7pIRAp4WMusmk_Mm96MmOGLm6aHWMusmk_Mm22ciAaLRPZoTFV4X5OImOGLm4lsmOGujMari7eZaH56Vrlsm0mcT-dITNlHmOuMsCmMi72YUXlsm0mMV_lsmOGujxtsmkirm0mci9aHWMusmOuMsCmbiOGLm_qbRMlsmOusmk_sgBuMgMmbi5lImOusmOGujM6siXF5iBO7i7WJhMuMgM64TK1YW8lsmOusmk_siOGLm2aHWMusmOuMsCurm0m8h5lImOusmOGuj1mrm0mMh5lImOusmOGuj_uMgMabRMlsmOusmk_siOGLm6aHWMusmOuMsCiri9uMgMibRMlsmOusmk_Mm12ciAuLmOGLm9aHWMusmOuMsCurm0m8U3lsmOusmk_chOGLm79ImOusmOGuj_uMgM_ImOusmOGuj_uMgMe4RMusmOuMsztMgMeITJdnQJlsmOGujxtsmkm8WINbh2q4h7u7WNZ4hPdIUMuMgMmrSMusmOuMsztMgMunSMusmk_Mm6WrQOCrh42YUXt8g_2si9usZgt8S3xoVAJ4ZMuMgMqYR7lsmOG_Q;c7bc6c188306b1bbfb092fbe39808788;tenjKJKT-JoRL1YRI9MT-J4S8ZIZ61YVF94WCeHTJJoTL9cQKxIWCeYU_tXW",
    "x-api-eid-token": "jdd03GNWKXUTIE6HBK6NALGSXSPDIJB4FOSTK2HH5HNAWZAUUMF5JYQCGOIB4252C7TECGDOSXSGLUC74Z5SQVPIKNU2LBEAAAAMYDRQO3ZYAAAAACJRQM6GTBDRNIQX",
    "loginType": "3",
    "uuid": "181111935.1751545604909534518369.1751545604.1752813314.1752822770.11",
    "type": "getstocks",
    "skuIds": "10155301055078,10155301055079,10155301055080,10155301055081,10155301055082,10155301055083,10155301055084,10155301055085,10155301055086,10155301055087,10155301055088,10155301055089,10155301055090,10155301055091,10155078248716,10155078248717,10155078248718,10155078248719,10155078248720,10155078248721,10155078248722,10155078248723,10155078248724,10155078248725,10155078248726,10155078248727,10155078248728,10155078248729,10155078248730,10155078248731,10155078248732,10155078248733,10155078248734,10155078248735,10155078248736,10155078248737,10155078248738,10155078248739,10155078248740,10155078248741,10155078248742,10155078248743",
    "area": "19_1607_4773_62123"
}
h5st = generate_h5st(params)
print(h5st)
print(len(h5st))
params['h5st'] = h5st
response = requests.get(url, headers=headers, cookies=cookies, params=params)
with open("stocks.json", "w", encoding="utf-8") as f:
    f.write(response.text)
print(response.text)
print(response)
